package com.cskaoyan.service.back;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.StorageImgVO;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 对象存储
 *
 * @auther 10-何兴绕
 * @Date 2022年10月12日 13:36
 */
@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    /**
     * 创建
     *
     * @param marketStorage
     * @return
     */
    @Override
    public StorageImgVO storageImg(MarketStorage marketStorage) {
        marketStorageMapper.insertSelective(marketStorage);
        MarketStorage m = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());
        StorageImgVO storageImgVO = new StorageImgVO(m.getId(), m.getKey(), m.getName(), m.getType(), m.getSize(), m.getUrl(), m.getAddTime(), m.getUpdateTime());
        return storageImgVO;
    }

    /**
     * 展示
     *
     * @param baseParam
     * @return
     */
    @Override
    public CommonData getStorageFile(BaseParam baseParam, String key, String name) {
        MarketStorageExample exampleCount = new MarketStorageExample();
        MarketStorageExample.Criteria criteriaCount = exampleCount.createCriteria();
        criteriaCount.andDeletedEqualTo(false);
        long count = marketStorageMapper.countByExample(exampleCount);
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketStorageExample example = new MarketStorageExample();
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        MarketStorageExample.Criteria criteria = example.createCriteria();
        if (key != null) {
            criteria.andKeyLike("%" + key + "%");
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);
        CommonData<MarketStorage> commonData = new CommonData<>();
        commonData.setTotal((int) count);
        commonData.setPages(marketStorages.size() / baseParam.getLimit() + 1);
        commonData.setLimit(baseParam.getLimit());
        commonData.setPage(baseParam.getPage());
        commonData.setList(marketStorages);
        return commonData;
    }

    /**
     * 更新
     *
     * @param marketStorage
     * @return
     */
    @Override
    public MarketStorage updateStorage(MarketStorage marketStorage) {
        marketStorage.setUpdateTime(new Date());
        int code = marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
        return marketStorage;
    }

    /**
     * 删除
     *
     * @param marketStorage
     * @return
     */
    @Override
    public int deleteStorage(MarketStorage marketStorage) {
        marketStorage.setDeleted(true);
        int code = marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
        return code;
    }
}
